/*----------------------------------------------------------------------------
 *      RL-ARM - TCPnet
 *----------------------------------------------------------------------------
 *      Name:    FTP_DEMO.C 
 *      Purpose: FTP Server demo example
 *----------------------------------------------------------------------------
 *      This code is part of the RealView Run-Time Library.
 *      Copyright (c) 2004-2011 KEIL - An ARM Company. All rights reserved.
 *---------------------------------------------------------------------------*/

#include <stdio.h>
#include <RTL.h>
#include <LPC17xx.h>                    /* LPC17xx definitions               */
#include "GLCD.h"
#include <Net_Config.h>


BOOL tick;
BOOL LCDupdate;
U32  dhcp_tout;
U8   lcd_text[2][16+1] = {" ",                /* Buffer for LCD text         */
                          "Waiting for DHCP"};
//
//extern LOCALM localm[];                       /* Local Machine Settings      */
//#define MY_IP localm[NETIF_ETH].IpAdr
//#define DHCP_TOUT   50                        /* DHCP timeout 5 seconds      */
/*--------------------------- dhcp_check ------------------------------------*/
//
//static void dhcp_check () {
//  /* Monitor DHCP IP address assignment. */
//
//  if (tick == __FALSE || dhcp_tout == 0) {
//    return;
//  }
//  if (mem_test (&MY_IP, 0, IP_ADRLEN) == __FALSE && !(dhcp_tout & 0x80000000)) {
//    /* Success, DHCP has already got the IP address. */
//    dhcp_tout = 0;
//    sprintf((char *)lcd_text[0]," IP address:");
//    sprintf((char *)lcd_text[1]," %d.%d.%d.%d", MY_IP[0], MY_IP[1],
//                                                MY_IP[2], MY_IP[3]);
//    LCDupdate = __TRUE;
//    return;
//  }
//  if (--dhcp_tout == 0) {
//    /* A timeout, disable DHCP and use static IP address. */
//    dhcp_disable ();
//    sprintf((char *)lcd_text[1]," DHCP failed    " );
//    LCDupdate = __TRUE;
//    dhcp_tout = 30 | 0x80000000;
//    return;
//  }
//  if (dhcp_tout == 0x80000000) {
//    dhcp_tout = 0;
//    sprintf((char *)lcd_text[0]," IP address:");
//    sprintf((char *)lcd_text[1]," %d.%d.%d.%d", MY_IP[0], MY_IP[1],
//                                                MY_IP[2], MY_IP[3]);
//    LCDupdate = __TRUE;
//  }
//}

/*--------------------------- init ------------------------------------------*/

static void init () {
  /* Add System initialisation code here */

//  /* Configure the GPIO for LEDs. */
//  LPC_GPIO1->FIODIR   |= 0xB0000000;
//  LPC_GPIO2->FIODIR   |= 0x0000007C;
//
//  /* Configure UART1 for 115200 baud. */
//  LPC_PINCON->PINSEL4 &= 0xFFFFFFF0;
//  LPC_PINCON->PINSEL4 |= 0x0000000A;
//  LPC_UART1->LCR = 0x83;
//  LPC_UART1->DLL = 9;                             /* 115200 Baud Rate @ 25.0 MHZ PCLK */
//  LPC_UART1->FDR = 0x21;                          /* FR 1,507, DIVADDVAL = 1, MULVAL = 2 */
//  LPC_UART1->DLM = 0;
//  LPC_UART1->LCR = 0x03;
  SystemInit();
	//turn of all Leds
  /* Configure the GPIO for LEDs. */
  LPC_GPIO1->FIODIR  &= ~0xB0000000;
  LPC_GPIO2->FIODIR   &= ~0x0000007C;
  /* Setup and enable the SysTick timer for 100ms. */
  SysTick->LOAD = (SystemCoreClock / 10) - 1;
  SysTick->CTRL = 0x05;
}


/*--------------------------- init_display ----------------------------------*/

static void init_display () {
  /* LCD Module init */

  GLCD_Init ();
  GLCD_Clear (Black);
  GLCD_SetBackColor(Black);
  GLCD_SetTextColor (White);
  GLCD_DisplayString (1, 4, "  Black Hat");
 // GLCD_DisplayString (2, 0, "FTP Bosch dowloader");
}


/*--------------------------- LED_out ---------------------------------------*/

void LED_out (U32 val) {
  const U8 led_pos[8] = { 28, 29, 31, 2, 3, 4, 5, 6 };
  U32 i,mask;

  for (i = 0; i < 8; i++) {
    mask = 1 << led_pos[i];
    if (val & (1<<i)) {
      if (i < 3) LPC_GPIO1->FIOSET = mask;
      else       LPC_GPIO2->FIOSET = mask;
    }
    else {
      if (i < 3) LPC_GPIO1->FIOCLR = mask;
      else       LPC_GPIO2->FIOCLR = mask;
    }
  }
}


/*--------------------------- timer_poll ------------------------------------*/

static void timer_poll () {
  /* System tick timer running in poll mode */

  if (SysTick->CTRL & 0x10000) {
    /* Timer tick every 100 ms */
    timer_tick ();
    tick  = __TRUE;
  }
}


/*--------------------------- sendchar --------------------------------------*/
//
//int sendchar (int ch)  {
//  /* Debug output to serial port. */
//
//  if (ch == '\n')  {
//    while (!(LPC_UART1->LSR & 0x20));
//    LPC_UART1->THR = 0x0D;
//  }
//  while (!(LPC_UART1->LSR & 0x20));
//  LPC_UART1->THR = (ch & 0xFF);
//  return (ch);
//}


/*--------------------------- getkey ----------------------------------------*/

int getkey (void) {
  /* A dummy function for 'retarget.c' */
  return (0);
}


/*--------------------------- blink_led -------------------------------------*/

static void blink_led () {
  /* Blink the LEDs on an eval board */
  const U8 led_val[16] = { 0x48,0x88,0x84,0x44,0x42,0x22,0x21,0x11,
                           0x12,0x0A,0x0C,0x14,0x18,0x28,0x30,0x50 };
  static U32 cnt;

  if (tick == __TRUE) {
    /* Every 100 ms */
    tick = __FALSE;
    LED_out (led_val[cnt]);
    if (++cnt >= sizeof(led_val)) {
      cnt = 0;
    }
  }
}


/*---------------------------------------------------------------------------*/

int main (void) {
  /* Main Thread of the TcpNet */

  init ();
  init_display ();
  init_TcpNet ();
  finit (NULL);

  while (1) {
    timer_poll ();
    main_TcpNet ();
   // blink_led ();
  }
}


/*----------------------------------------------------------------------------
 * end of file
 *---------------------------------------------------------------------------*/
